#// *************************************************************************
#// * COPYRIGHT 2004, EMULEX CORPORATION                                    *
#// * 3333 Susan Street, Costa Mesa, CA 92626                               *
#// *                                                                       *
#// * All rights reserved.  This computer program and related documentation *
#// * is protected by copyright  and distributed under licenses restricting *
#// * its use,  copying,  distribution  and decompilation.    This computer *
#// * program  and its  documentation  are CONFIDENTIAL  and a TRADE SECRET *
#// * of EMULEX CORPORATION.   The receipt or  possession of  this  program *
#// * or its documentation does not  convey rights to reproduce or disclose *
#// * its  contents,  or to  manufacture, use, or sell anything that it may *
#// * describe, in whole or in part,  without the specific  written consent *
#// * of  EMULEX CORPORATION.   Any reproduction  of  this program  without *
#// * the express  written  consent  of EMULEX  CORPORATION  is a violation *
#// * of the  copyright laws  and may  subject you to  criminal prosecution.*
#// *************************************************************************


opt="$1"

SSCInstall=0

# Check if HBAnywareSSC install option is passed in
if [ -n "$opt" ];then
    opt=`echo $opt | tr A-Z a-z`
    if [ "$opt" = "ssc" ];then
        SSCInstall=1
    fi
fi

#
# This scripts installs HBAnyware for Linux
#
                                                                                                                        
# Get environment data
OS=`uname -s`
Arch=`uname -m`
if [ "$Arch" = "i686" ];then
    Arch="i386"
fi
if [ "$Arch" = "x86_64" ];then
    Arch="i386"
    X86_64=1
else
    X86_64=0
fi

if [ "$OS" = "Linux" ];then
        Kernel_Ver=`uname -r 2>&1 | awk '{ print $1 }' | awk -F. '{ print $1$2 }'`
fi

# This is a Linux install only, if we're not on Linux then stop
if [ "$OS" != "Linux" ];then
    echo "This installation is for Linux only! Exiting..."
    exit
fi
                                                           
# Only proceed with SSC install if HBAnyware is installed
if [ $SSCInstall -eq 1 ];then
    Cur_HBA=`rpm -qa | grep HBAnyware | grep -v SSC`
    if [ -z "$Cur_HBA" ];then
        echo "ERROR: HBAnyware not installed. HBAnyware must be installed before SSC can be installed"
        exit
    fi
fi

cd enterprise_kitfiles

# Untar and uncompress tgz file
echo "Untarring EmlxApps tgz file..."
tar xfz EmlxApps*.tgz
##echo ""

GCC_Ver=`gcc -v 2>&1 | grep version | awk '{ print $3 }' | awk -F. '{ print $1 }'`
OS_Type=`if [ -d /usr/src/redhat ];then echo "RH";else echo "SL";fi`
if [ "$OS_Type" = "RH" ];then
    if [ "$Kernel_Ver" = "26" ]; then
	OS_Type="RH4"
	OS_Ver="rhel-4.0"
    else
        if [ "$GCC_Ver" -eq 2 ];then
            OS_Ver="rhel-2.1"
        else
            OS_Ver="rhel-3.0"
        fi
    fi
else
    if [ "$Kernel_Ver" = "26" ]; then
	OS_Type="SL9"
    	OS_Ver="sles-9.0"
    else
    	OS_Ver="sles-8.0"
    fi
fi

                                                                                                                          
# HBAnyware Installation
if [ $SSCInstall -eq 0 ];then

    # Make sure this platform is supported
    rpm_dir="./${Arch}_${OS_Type}_${GCC_Ver}.XX"
    if [ ! -d "$rpm_dir" ];then
        echo "ERROR: $rpm_dir not found"
        echo "This apps kit does not support this platform"
        exit
    fi

    # Uninstall any previous HBAnyware install
    Cur_HBA=`rpm -qa | grep HBAnyware | grep -v SSC`
    if [ -n "$Cur_HBA" ];then
        echo "Uninstalling HBAnyware -- $Cur_HBA -- ..."
        rpm -e --nodeps "$Cur_HBA"
        echo ""
    fi
 

    # Install dfc libs and utility
    dfc_test=`find . -name lpfcutil*.tgz`
    if [ -n "$dfc_test" ];then
       echo "Untarring lpfcutil tgz file..."
       tar xfz lpfcutil*.tgz
	else
       echo "Untarring lpfcutil .tar.gz file..."
       tar xfz lpfcutil*.tar.gz
	fi
    if [ ! -d /usr/sbin/lpfc ];then
        mkdir /usr/sbin/lpfc
    fi
    if [ "$Arch" = "ppc64" ]; then
        cp -f ./lpfcutil/ppc/${OS_Ver}/lib* /usr/lib
        cp -f ./lpfcutil/ppc64/${OS_Ver}/lib* /usr/lib64
        cp -f ./lpfcutil/ppc/${OS_Ver}/dfc /usr/sbin/lpfc
    else
        cp -f ./lpfcutil/${Arch}/${OS_Ver}/lib* /usr/lib
        cp -f ./lpfcutil/${Arch}/${OS_Ver}/dfc /usr/sbin/lpfc
    fi
    # If amd64 then install 64bit libs also
    if [ $X86_64 -eq 1 ];then
        cp -f ./lpfcutil/x86_64/${OS_Ver}/lib* /usr/lib64
    fi

    if [ -f "./scripts/lun_scan" ]; then
	    cp -f ./scripts/lun_scan /usr/sbin/lpfc
	else
    	if [ -f "./lun_scan" ]; then
	        cp -f ./lun_scan /usr/sbin/lpfc
		fi
    fi

    # Install multipulse library
    mpl_test=`find . -name libmpl*.tgz`
    if [ -n "$mpl_test" ];then
       echo "Untarring libmpl tgz file..."
       tar xfz libmpl*.tgz
	else
       echo "Untarring libmpl .tar.gz file..."
       tar xfz libmpl*.tar.gz
	fi
    if [ "$Arch" = "ppc64" ]; then
        cp -f ./lpfcutil/ppc/${OS_Ver}/lib* /usr/lib
        cp -f ./lpfcutil/ppc64/${OS_Ver}/lib* /usr/lib64
    else
        cp -f ./lpfcutil/${Arch}/${OS_Ver}/lib* /usr/lib
    fi

    # Install HBAAPI libs
    echo "Untarring hbaapi libs tgz file..."
    tar xfz hbaapi*.tgz
    if [ "$Arch" = "i386" ]; then
        cp -f ./hbaapi/${Arch}/32bit/lib* /usr/lib
    fi
    if [ "$Arch" = "ia64" ]; then
        cp -f ./hbaapi/${Arch}/64bit/lib* /usr/lib
    fi
    if [ "$Arch" = "ppc64" ]; then
        cp -f ./hbaapi/${Arch}/32bit/lib* /usr/lib
        if [ "$Kernel_Ver" = "26" ]; then
            cp -f ./hbaapi/${Arch}/64bit/lib* /usr/lib64
        else
            cp -f ./hbaapi/${Arch}/64bit/${OS_Ver}/lib* /usr/lib64
		fi
    fi
    if [ $X86_64 -eq 1 ]; then
        cp -f ./hbaapi/amd64/64bit/lib* /usr/lib64
    fi


    # Install lputil utility
    echo "Untarring lputil tgz file..."
    tar xfz lputil*.tgz
    cp -f ./lputil/${Arch}/lputil* /usr/sbin/lpfc
    chmod +x /usr/sbin/lpfc/lputil*


    # Install HBAnyware
    cd "$rpm_dir"
    rpm -U --nodeps HBAnyware-*.rpm
    cd ..
    echo ""

    # Install JRE
    cur_dir=`pwd`
    if [ $X86_64 -eq 1 ];then
        jre_dir="./jre_amd64"
    else
        jre_dir="./jre_${Arch}"
    fi
    if [ ! -d "$jre_dir" ];then
        echo "ERROR: JRE dir $jre_dir not found. Local JRE can not be installed"
        exit
    fi
    if [ ! -d /usr/sbin/hbanyware/jre ];then
        mkdir /usr/sbin/hbanyware/jre
    fi
    cd /usr/sbin/hbanyware/jre
    echo "Untarring HBAnyware local jre..."
    tar xfz ${cur_dir}/${jre_dir}/jre_hbanyware.tgz
    cd "$cur_dir"
    echo ""


    echo "HBAnyware installation complete."
    echo ""



#  HBAnywareSSC Installation
else   
    # Uninstall any previousely installed HBAnywareSSC
    Cur_HBA=`rpm -qa | grep HBAnywareSSC`
    if [ -n "$Cur_HBA" ];then
        echo "Uninstalling HBAnywareSSC -- $Cur_HBA -- ..."
        rpm -e "$Cur_HBA"
    fi

    # Install HBAnywareSSC
    rpm_dir="./${Arch}_${OS_Type}_${GCC_Ver}.XX"
    cd "$rpm_dir"
    rpm -U --nodeps HBAnywareSSC-*.rpm
    cd ..
    echo ""

    echo "HBAnywareSSC installation complete."
    echo ""
fi


# Clean up
echo "Cleaning up..."
rm -rf ./*.XX
rm -rf ./jre_*
rm -rf ./etc
rm -rf lputil
rm -rf hbaapi
rm -rf lpfcutil
echo ""

cd ..

